package com.gtlab1207.br_awareness.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class DateUtil {
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 将时间格式化为毫秒级字符串，防止文件重复
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String d = formatter.format(date);
        return d;
    }


    /**
     * 获取系统当前时间 到毫秒yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date presentTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );

        Date time=null;
        try {
            time= sdf.parse(sdf.format(new Date()));

        } catch (ParseException e) {

            e.printStackTrace();
        }
        return time;
    }

    /**
     * 获取系统当前时间 到毫秒yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String presentTimeStr() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );

        String time=null;

        time= sdf.format(new Date());


        return time;
    }


    /** 001
     * 参数map添加时间
     * @param params
     * @param beginTime
     * @param endTime
     * @return
     */
    public static Map<String, Object> addTime(Map<String, Object> params, String beginTime, String endTime) {
        if (beginTime != null && beginTime.trim() != "") {
            params.put("beginTime", beginTime + " 00:00:00");
        }
        if (endTime != null && endTime != "") {
            params.put("endTime", endTime + " 23:59:59");
        }
        return params;
    }



    /**
     * 计算两个日期内的天数
     * @param startTime 起始日期
     * @param endTime 结束日期
     * @return
     */
    public static Integer longOfTwoDate(Date startTime,Date endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        startTime = simpleDateFormat.parse(simpleDateFormat.format(startTime));
        endTime = simpleDateFormat.parse(simpleDateFormat.format(endTime));
        int timeNum = 3600 * 24;
        Calendar startCal = Calendar.getInstance();
        Calendar endCal = Calendar.getInstance();
        startCal.setTime(startTime);
        endCal.setTime(endTime);
        startCal.compareTo(endCal);
        //得到两个日期相差的天数
        int nums = ((int)(endCal.getTime().getTime()/1000)-(int)(startCal.getTime().getTime()/1000))/timeNum;//此处修改日期单位
        return nums;
    }

    /**
     * 计算某日期是星期几
     * @param pTime
     * @return
     * @throws Throwable
     */
    public static Integer dayForWeek(String pTime) throws Throwable {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date tmpDate = format.parse(pTime);
        Calendar cal = Calendar.getInstance();
        Integer[] weekDays = { 0, 1, 2, 3, 4, 5, 6 };
        try {
            cal.setTime(tmpDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 指定日期加上天数后的日期
     * @param num 为增加的天数
     * @param newDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num,String newDate) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(newDate);
        //System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        //System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }

    /**
     * 指定日期加上小时后的日期
     * @param num 为增加的小时数
     * @param newDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String plusHour(int num,String newDate) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date currdate = format.parse(newDate);
        //System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.HOUR, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        //System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }

    /**
     * 计算两个日期之间相差的年数
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static Integer betweenYear(Date startTime,Date endTime){
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(startTime);
        aft.setTime(endTime);
        int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int year = aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR);
        if(result < 0){
            result = -1;
        }else if(result == 0){
            result = surplus < 0 ? -1 : 0;
        }else{
            result = 0;
        }
        return year+result;
    }

    // 获得某天最大时间 2017-10-15 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获得某天最小时间 2017-10-15 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
}
